package com.budwk.app.miniapp.services.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.miniapp.models.WxMsg;
import com.budwk.app.miniapp.services.WxSqfwFireHiddenService;
import com.budwk.app.miniapp.utils.HttpUtils;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.google.common.collect.Maps;
import org.nutz.dao.Dao;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.Map;

@IocBean(args = {"refer:dao"})
public class WxSqfwFireHiddenServiceImpl extends BaseServiceImpl<WxMsg> implements WxSqfwFireHiddenService {
    public WxSqfwFireHiddenServiceImpl(Dao dao) {
        super(dao);
    }
    @Inject
    private PropertiesProxy propertiesProxy;
    @Override
    public Pagination getFireHiddenPdfList(String phoneNo, Integer pageNo, Integer pageSize) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("phoneNo", phoneNo);
        paramMap.put("pageNo", pageNo);
        paramMap.put("pageSize", pageSize);
        String responseStr = HttpUtils.postRequest("https://app.xyasst.com/dfxyapi/platform/sqfw/completeflow/getFireHiddenPDFByPhoneNo", paramMap);
        JSONObject responseJson = JSONObject.parseObject(responseStr);

        Pagination pagination = new Pagination();
        Integer code = responseJson.getInteger("code");
        if (code != 0) {
            pagination.setPageSize(pageSize);
            pagination.setPageNo(pageNo);
            return pagination;
        }
        JSONObject data = responseJson.getJSONObject("data");
        JSONArray list = data.getJSONArray("list");
        list.forEach(s->{
            JSONObject obj  =(JSONObject)s;
            String resourcePath=obj.getString("resourcePath");
            if(!resourcePath.startsWith("https")){
                obj.put("resourcePath",propertiesProxy.get("huawei-oss.url")+obj.getString("resourcePath"));
            }
        });
        Integer totalCount = data.getInteger("totalCount");
        pagination.setTotalCount(totalCount);
        pagination.setPageSize(pageSize);
        pagination.setPageNo(pageNo);
        pagination.setList(list);
        return pagination;
    }
}
